package example.pattern.visitor;

import java.util.ArrayList;
import java.util.List;

public class Directory extends Entry {
	List<Entry> entries  = new ArrayList<Entry>();
	private Directory parentDir;
	public static final String SEPARATOR = System.getProperty("file.separator");
	private String name;
	
	
	public Directory(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void addFile(File file){
		entries.add(file);
		file.setDirectory(this);
	}
	
	public void addDirectory(Directory direcotry){
		entries.add(direcotry);
		direcotry.setParentDir(this);
		
	}
	
	
	public Directory getParentDir() {
		return parentDir;
	}

	public void setParentDir(Directory parentDir) {
		this.parentDir = parentDir;
	}

	public String getPath(){
		StringBuilder sb = new StringBuilder();
		sb.append(this.getName());
		Directory dir = this.getParentDir();
		while(dir != null){
			sb.insert(0, dir.getName() + SEPARATOR);
			dir = dir.getParentDir();
		}
		return sb.toString();
	}
	
	@Override
	public void acceptVisitor(Visitor visitor){
		super.acceptVisitor(visitor);
		for(Entry entry: entries){
			entry.acceptVisitor(visitor);
		}
		this.visitor.visit(this);
	}
	
}
